<?php
function slimserver_configure() {
	global $config;

	if (isset($config['slimserver']['enable'])) {
		slimserver_start();
	}
	else {
		slimserver_stop();
	}

	return 0;
}

function slimserver_start() {
	global $config, $g;

	if ($g['booting'])
		echo "Starting Logitech Media Server... ";

	/* run Logitech Media Server if it's not running yet */
	if (!file_exists("{$config['slimserver']['rootfolder']}/var/squeezebox.pid")) {
		$g['starting_sc'] = 1;
		mwexec("HOME={$config['slimserver']['rootfolder']} PAR_GLOBAL_CLEAN=1" .
					" TEMP={$config['slimserver']['rootfolder']}/var/squeezebox/Cache TMPDIR={$config['slimserver']['rootfolder']}/var/squeezebox/Cache" .
					" /usr/local/bin/perl {$config['slimserver']['rootfolder']}/squeezebox/slimserver.pl" .
					" --user=www --daemon --pidfile={$config['slimserver']['rootfolder']}/var/squeezebox.pid");
	}

	if ($g['booting'])
		echo "done\n";
}

function slimserver_stop() {
	global $config;

	/* kill any running slimserver.pl */
	killbypid("{$config['slimserver']['rootfolder']}/var/squeezebox.pid");
	/* sometimes SC doesn't shut down properly - give it a moment to think about, then kill it */
	sleep(5);
	killbyname("perl");
}

function squeezebox_restart() {
	slimserver_stop();
	sleep(5);
	slimserver_initfolders(true);
	slimserver_start();
}

function slimserver_initfolders($killpidfile) {
	global $config;

	if (!is_dir($config['slimserver']['rootfolder'])) {
		return;
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/etc")) {
		mkdir("{$config['slimserver']['rootfolder']}/etc");
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/etc/squeezebox")) {
		mkdir("{$config['slimserver']['rootfolder']}/etc/squeezebox");
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/var")) {
		mkdir("{$config['slimserver']['rootfolder']}/var");
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/var/squeezebox")) {
		mkdir("{$config['slimserver']['rootfolder']}/var/squeezebox");
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/var/squeezebox/Logs")) {
		mkdir("{$config['slimserver']['rootfolder']}/var/squeezebox/Logs");
	}

	if (!is_dir("{$config['slimserver']['rootfolder']}/var/squeezebox/Cache")) {
		mkdir("{$config['slimserver']['rootfolder']}/var/squeezebox/Cache");
	}

	if (!is_dir("/usr/local/www/ext")) {
		mkdir("/usr/local/www/ext");
	}

	if (!is_dir("/usr/local/www/ext/slimserver")) {
		mkdir("/usr/local/www/ext/slimserver");
	}


	mwexec("/usr/sbin/chown -R www:wheel {$config['slimserver']['rootfolder']}/squeezebox");
	mwexec("/usr/sbin/chown -R www:wheel {$config['slimserver']['rootfolder']}/var");
	mwexec("/usr/sbin/chown -R www:wheel {$config['slimserver']['rootfolder']}/etc");
	mwexec("/bin/chmod -R a+w {$config['slimserver']['rootfolder']}");
	mwexec("/bin/chmod 644 {$config['slimserver']['rootfolder']}/squeezebox/MySQL/my.tt");
	mwexec("/bin/chmod 644 {$config['slimserver']['rootfolder']}/var/squeezebox/Cache/my.cnf");
	mwexec("/bin/chmod -R g+w /ftmp");
	mwexec("/bin/chmod -R g+w /tmp");

	mwexec("/bin/ln -fs {$config['slimserver']['rootfolder']}/usr/lib/* /usr/local/lib");
	mwexec("/bin/ln -fs /usr/local/lib/libvorbisfile.so.6 /usr/local/lib/libvorbisfile.so.5");
	mwexec("/bin/ln -fs {$config['slimserver']['rootfolder']}/usr/bin/* /usr/local/bin");
	mwexec("/bin/cp -R -f {$config['slimserver']['rootfolder']}/usr/www/*.inc /usr/local/www/ext/slimserver/");
	mwexec("/bin/cp -R -f {$config['slimserver']['rootfolder']}/usr/www/*.php /usr/local/www/");
	mwexec("/bin/ln -fs {$config['slimserver']['rootfolder']}/usr/share/mysql/errmsg.* {$config['slimserver']['rootfolder']}/squeezebox/MySQL/");

	# set up Slim/Utils/OS/Custom.pm
	mwexec("/bin/ln -fs {$config['slimserver']['rootfolder']}/usr/share/Slim/SlimNAS.pm {$config['slimserver']['rootfolder']}/squeezebox/Slim/Utils/OS/Custom.pm");

	# remove .pid file on systemstart to recover from crashes etc.
	if ($killpidfile) {
		unlink_if_exists("{$config['slimserver']['rootfolder']}/var/squeezebox.pid");
	}
}

function slimserver_register() {
	global $config;

	$i = 0;
	
	if (is_array($config['rc']['postinit']['cmd'])) {
		for ($i; $i < count($config['rc']['postinit']['cmd']); $i++) {
			if (preg_match('/services_slimserver_init\.php/', $config['rc']['postinit']['cmd'][$i]))
				break;
		}
	}

	$config['rc']['postinit']['cmd'][$i] = "/usr/local/bin/php -f {$config['slimserver']['rootfolder']}/usr/www/services_slimserver_init.php";


	$i = 0;
	if (is_array($config['rc']['shutdown']['cmd'])) {
		for ($i; $i < count($config['rc']['shutdown']['cmd']); $i++) {
			if (preg_match('/services_slimserver_stop\.php/', $config['rc']['shutdown']['cmd'][$i]))
				break;
		}
	}

	$config['rc']['shutdown']['cmd'][$i] = "/usr/local/bin/php -f {$config['slimserver']['rootfolder']}/usr/www/services_slimserver_stop.php";

	write_config();
}

function slimserver_unregister() {
	global $config;

	slimserver_stop();

	# remove startup/shutdown items

	$i = 0;
	if (is_array($config['rc']['postinit']['cmd'])) {
		for ($i; $i < count($config['rc']['postinit']['cmd']); $i++) {
			if (preg_match('/services_slimserver_init\.php/', $config['rc']['postinit']['cmd'][$i]))
				unset($config['rc']['postinit']['cmd'][$i]);
		}
	}

	$i = 0;
	if (is_array($config['rc']['shutdown']['cmd'])) {
		for ($i; $i < count($config['rc']['shutdown']['cmd']); $i++) {
			if (preg_match('/services_slimserver_stop\.php/', $config['rc']['shutdown']['cmd'][$i]))
				unset($config['rc']['shutdown']['cmd'][$i]);
		}
	}

	# remove Logitech Media Server related settings - store folder location temporarily
	$slimroot = $config['slimserver']['rootfolder'];
	unset($config['slimserver']);
	write_config();

	foreach (glob("{$slimroot}/usr/bin/*") as $file) {
		$file = str_replace("{$slimroot}/usr/bin", "/usr/local/bin", $file);
		if (is_link($file)) {
			unlink($file);
		}
	}

	foreach (glob("{$slimroot}/usr/lib/*") as $file) {
		$file = str_replace("{$slimroot}/usr/lib", "/usr/local/lib", $file);
		if (is_link($file)) {
			unlink($file);
		}
	}

	foreach (glob("{$slimroot}/squeezebox/MySQL/errmsg.*") as $file) {
		if (is_link($file)) {
			unlink($file);
		}
	}

	foreach (glob("{$slimroot}/usr/www/*") as $file) {
		$file = str_replace("{$slimroot}/usr/www", "/usr/local/www", $file);
		if (is_link($file)) {
			unlink($file);
		}
	}

	foreach (glob("/usr/local/www/ext/slimserver/*") as $file) {
		unlink($file);
	}

	if (is_dir("/usr/local/www/ext/slimserver")) {
		rmdir("/usr/local/www/ext/slimserver");
	}

	mwexec("/bin/chmod -R 777 {$slimroot}");
}

function check_slimserver() {
	global $config;
	return file_exists($config['slimserver']['rootfolder'] . "/squeezebox");
}

function get_nice_filesize ($file) {
	$dsize = filesize($file);
	if (strlen($dsize) <= 9 && strlen($dsize) >= 7) {
		$dsize = number_format($dsize / 1048576,1);
		return "$dsize MB";
	}
	elseif (strlen($dsize) >= 10) {
		$dsize = number_format($dsize / 1073741824,1);
		return "$dsize GB";
	}
	else {
		$dsize = number_format($dsize / 1024,1);
		return "$dsize KB";
	}
}
?>
